﻿var classjeu;
//Création du perso, des animaux et des fruits
var perso = new Personnage(100, 80);
var animaux = new Array(new Animal(250, 280, "dino"), new Animal(650, 580, "dino"), new Animal(600, 80, "poule"), new Animal(50, 480, "oiseau"), new Animal(300, 180, "poule"), new Animal(150, 380, "oiseau"));
var fruits = new Array(new Fruit("banane",200), new Fruit("cerise",100), new Fruit("citron",350), new Fruit("fraise",250), new Fruit("mandarine",100), new Fruit("pasteque",400), new Fruit("pomme",300));

function Jeu() {
    classjeu = this;

    this.jeux = document.getElementById("jeux");
    this.score = 0;
    this.carte = new Array();
    this.test = document.getElementById("test");


    //Creer la matrice sur laquelle on va jouer
    for (var i = 0; i < 16; i++) {
        this.carte[i] = new Array();
        for(var u = 0; u < 16; u++){
            this.carte[i][u] = new Case(i*50,u*50);
        }
    }

    //Génération des cases déjà creusées (Dites Predeterminées)
    for (var o = 0; o < 20; o++) {
        var rand1 = Math.floor(Math.random() * 15);
        var rand2 = Math.floor(Math.random() * 15);
        this.carte[rand1][rand2] = new CasePredetermine(rand1 * 50, rand2 * 50);
        for (var p = 0; p < 2; p++) {
            var rand3 = Math.round(Math.random());
            var rand4 = Math.round(Math.random());
            this.carte[rand1 + rand3][rand2 + rand4] = new CasePredetermine((rand1 + rand3) * 50, (rand2 + rand4) * 50);
        }
    }

    //Cas du gameover
    this.gameover = function () {
        window.clearInterval(this.timer);
        perso.cote_sprite.src = "images/personnages/perso13.png";
        document.getElementById("all").onkeydown = function (event) {
            if (event.keyCode == 13) {
                window.location = "jeu.html";
            }
        }
        document.getElementById("gameover").style.display = 'inline';
    }

    //Cas de la victoire
    this.victoire = function () {
        window.clearInterval(this.timer);
        document.getElementById("all").onkeydown = function (event) {
            if (event.keyCode == 13) {
                window.location = "jeu.html";
            }
        }
        document.getElementById("victory").style.display = 'inline';
    }

    //Fait bouger les animaux à chaque cycle
    this.move = function () {
        for (var i in animaux) {
            animaux[i].move();
        }
    }

    //Génère le terrain
    this.genererTerrain = function () {
        this.carte[perso.X / 50][(perso.Y + 20) / 50].changeEtat();
        for (var i in animaux) {
            this.carte[animaux[i].X / 50][(animaux[i].Y + 20) / 50].changeEtat();
        }
    }

    this.init = function () {
        this.dateK = 0;
        this.genererTerrain();

        document.getElementById("all").onkeydown = function (event) {
            if (new Date().getTime() - classjeu.dateK > 100) {
                perso.move(event);
                classjeu.dateK = new Date().getTime();
            }
        }

        this.timer = window.setInterval("classjeu.move()", 500);
    }
    this.init();

}